/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-9
 */
package com.flyee.controller{
	import com.flyee.io.Config;
	import com.flyee.model.Scene;
	import com.flyee.service.CharactorService;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.Obsevable;
	import com.flyee.utils.Obsever;
	import com.flyee.utils.gs.TweenMax;
	import com.flyee.utils.load.StackLoader;
	import com.flyee.utils.load.processor.LoadLevelType;
	import com.flyee.utils.load.processor.LoadReturnType;
	import com.flyee.viewer.ComponentViewer;
	import com.flyee.viewer.SceneViewer;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * 场景控制器 控制场景视觉元素的行为：加载，卸载，滚屏等 
	 */
	public class SceneController extends Obsevable implements Obsever{
		
		[Inject]
		public var charactorService : CharactorService;
		
		private var TweenRunning : Boolean = false;
		
		public function SceneController(){
			
		}
		
//		public static var TweenRunning : Boolean = false; 
		
		/**
		 * 接到被观察者通知后执行
		 * @param o 被观察者
		 * @param command 指令
		 * @param args 参数
		 */
		public function execute(o : Obsevable, command : String=null, args : * = null) : void {
			switch(command){
				case "addComponent" : 
					addComponent(args[0], args[1], args[2], args[3]);
					break ;
				case "scrollWindow" : 
					scrollWindow(args[0], args[1]);
					break ;
				default :
//					MyLog.debug("command is null!");
					break;
			}
		}
		
		/**
		 * 加载场景UI 
		 */
		public function loadScene(scene : Scene) : void {
			ObjectCollection.currentSceneViewer = new SceneViewer();
			
			var loader : StackLoader = new StackLoader();
			loader.load(scene.bgUrl, LoadLevelType.LV_3, null, loadSceneHandler, LoadReturnType.RETURN_LOADER, null);
			//加载地图数据
			var urlR : URLRequest = new URLRequest(Config.getInstance().getResourcePath() + "resource/" + scene.id + ".map");
        	var load : URLLoader = new URLLoader(urlR);
        	load.dataFormat = URLLoaderDataFormat.BINARY
        	load.addEventListener(Event.COMPLETE, loadSceneDataHandler);
		}
		
		public function loadSceneHandler(loader : Loader) : void {
			ObjectCollection.currentSceneViewer.backgroundLayer.addChild(loader);
			ObjectCollection.sceneContainer.addChild(ObjectCollection.currentSceneViewer);
		}
		
		public function loadSceneDataHandler(event : Event) : void {
			if(ObjectCollection.mapData.map != null){
				while(ObjectCollection.mapData.map.length>0){
					var arr : Array = ObjectCollection.mapData.map.shift() as Array;
					if(arr != null){
						while(arr.length >0){
							arr.shift();
						}
					}
				}
			}
			var byteData : ByteArray = event.target.data as ByteArray;
        	byteData.uncompress();
        	var mapData : Array = byteData.readObject() as Array;
        	ObjectCollection.mapData.map = mapData;
			charactorService.initAvatars(ObjectCollection.bufferArray);		
        	ObjectCollection.currentSceneViewer.addEventListener(MouseEvent.CLICK, charactorService.requestPath);
		}
		
		public function unloadScene() : void {
			ObjectCollection.currentSceneViewer.removeEventListener(MouseEvent.CLICK, charactorService.requestPath);
			ObjectCollection.sceneContainer.removeChild(ObjectCollection.currentSceneViewer);
			ObjectCollection.currentScene = null;
			ObjectCollection.currentSceneViewer.dispose();
			ObjectCollection.currentSceneViewer = null;
			for each(var mid : String in ObjectCollection.componentMap.keys()){
				ObjectCollection.removeRelationByMid(mid);
			}
			ObjectCollection.componentMap.clear();
			for each(var vid : String in ObjectCollection.componentViewerMap.keys()){
				ObjectCollection.removeRelationByVid(vid);
				//通知观察者执行
				setChanged();
				notifyObsevers("unloadComponent", ObjectCollection.componentViewerMap.get(vid));
			}
			ObjectCollection.componentViewerMap.clear();
			
		}
		
		/**
		 * 在场景UI各层中添加组件UI
		 * @param componentUI 要加入场景的组件
		 * @param layer 要加入的层
		 * @param x 指定组件X坐标 
		 * @param y 指定组件Y坐标
		 */
		public function addComponent(componentUI : ComponentViewer, layer : int, x : int, y : int) : void {
			//加入不同的层中
			switch(layer){
				case 1 : 
					ObjectCollection.currentSceneViewer.driftLayer.addChild(componentUI);					
					break;
				case 2 : 
					ObjectCollection.currentSceneViewer.componentLayer.addChild(componentUI);
					break;
				case 3 : 
					ObjectCollection.currentSceneViewer.landLayer.addChild(componentUI);
					break;
				default : 
 					break;
			}
			componentUI.x = x;
			componentUI.y = y;
		}
		
		/**
		 * 地图滚屏 
		 * @param directionX X方向
		 * @param directionY Y方向
		 */
		public function scrollWindow(directionX : int, directionY : int) : void {
			if(TweenRunning) return ;
			var xNew : Number = ObjectCollection.currentSceneViewer.x;
			var yNew : Number = ObjectCollection.currentSceneViewer.y;
			if(directionX==1){
				if(xNew + 300 > 0){
					xNew = 0;
				}else{
					xNew += 300;
				}
			}
			if(directionX==-1){
				if(ObjectCollection.currentSceneViewer.backgroundLayer.width - 1000 + xNew < 300){
					xNew = - ObjectCollection.currentSceneViewer.backgroundLayer.width + 1000;
				}else{
					xNew -= 300;
				}
			}		
			if(directionY==1){
				if(yNew + 200 > 0){
					yNew = 0;
				}else{
					yNew += 200;
				}
			}
			if(directionY==-1){
				if(ObjectCollection.currentSceneViewer.backgroundLayer.height - 600 + yNew < 200){
					yNew = - ObjectCollection.currentSceneViewer.backgroundLayer.height + 600;
				}else{
					yNew -= 200;
				}				
			}				
			
			TweenRunning = true;
			
			TweenMax.to(ObjectCollection.currentSceneViewer, 1, 
							{x:xNew, 
							 y:yNew, 
							 onComplete:tweenCompleteHandler});
		}
		
		public function tweenCompleteHandler() : void {
			TweenRunning = false;
		}

	}
	
}